<?php

$pid_file_name = "/var/run/smpprouter.pid";
$bin_path = "smpprouter";
$log_file = "/var/log/smpprouter.log";
$stat_file = "/usr/local/etc/smpprouter/stat.html";
$pid = false;

function getSessions($stat_file) {
  if(file_exists($stat_file))
    echo file_get_contents($stat_file);
}

function getLog($log_file) {
  if(file_exists($log_file)) {
    header("Content-type: text/html");
    echo file_get_contents($log_file);
  }
}

function clearLog($log_file) {
  if(file_exists($log_file)) {
    exec("sudo /bin/echo \"\" > " . $log_file);
  }
}

function getLogSize($log_file) {
  $result = "0 B";
  if(file_exists($log_file)) {
    $bytes = floatval(filesize($log_file));
    
    $arr_bytes = array(
      0 => array(
        "UNIT" => "TB",
        "VALUE" => pow(1024, 4)
      ),
      1 => array(
        "UNIT" => "GB",
        "VALUE" => pow(1024, 3)
      ),
      2 => array(
        "UNIT" => "MB",
        "VALUE" => pow(1024, 2)
      ),
      3 => array(
        "UNIT" => "KB",
        "VALUE" => 1024
      ),
      4 => array(
        "UNIT" => "B",
        "VALUE" => 1
      ),
    );

    foreach($arr_bytes as $arr_item) {
      if($bytes >= $arr_item["VALUE"]) {
        $result = $bytes / $arr_item["VALUE"];
        $result = str_replace(".",  ",",strval(round($result, 2))) .
                  " " . $arr_item["UNIT"];
        break;
      }
    }
  }
  echo $result;
}

function getProcessInfo($pid) {
  $ps_info = array();

  if($pid) {
    $ps_out = null;

    exec("sudo ps -aux -p $pid", $ps_out);

    if(count($ps_out) == 2) {
      $ps_arr = preg_split('/\s+/', $ps_out[1]);

      $ps_info["pid"]         = $ps_arr[1];
      $ps_info["cpu_percent"] = $ps_arr[2];
      $ps_info["mem_percent"] = $ps_arr[3];
      $ps_info["mem_virtual"] = $ps_arr[4];
      $ps_info["mem_page"]    = $ps_arr[5];
      $ps_info["start_time"]  = $ps_arr[8];
      $ps_info["command"]     = $ps_arr[10];
    }
  }

  echo json_encode($ps_info);
}

function startProcess($pid, $proc) {
  if(!$pid)
    exec("sudo $proc");
}

function stopProcess($pid) {
  if($pid)
    exec("sudo kill $pid");
}

function restartProcess($pid, $proc) {
  if($pid) {
    exec("sudo kill $pid");
  }
  exec("sudo $proc");
}

function reloadConfig($pid) {
  if($pid)
    exec("sudo kill -SIGUSR1 $pid");
}

if(isset($_GET["c"])) {
  $cmd = $_GET["c"];
  
  if(file_exists($pid_file_name)) {
    $pid = exec("sudo cat $pid_file_name");
    if(strlen($pid) <= 1)
      $pid = false;
  }  
  
  if(strcmp($cmd, "ps") == 0) {
    getProcessInfo($pid);
  } else if(strcmp($cmd, "start") == 0) {
    startProcess($pid, $bin_path);
  } else if(strcmp($cmd, "stop") == 0) {
    stopProcess($pid);
  } else if(strcmp($cmd, "restart") == 0) {
    restartProcess($pid, $bin_path);
  } else if(strcmp($cmd, "reconfig") == 0) {
    reloadConfig($pid);
  } else if(strcmp($cmd, "logsize") == 0) {
    getLogSize($log_file);
  } else if(strcmp($cmd, "sessions") == 0) {
    getSessions($stat_file);
  } else if(strcmp($cmd, "log") == 0) {
    getLog($log_file);
  } else if(strcmp($cmd, "clearlog") == 0) {
    clearLog($log_file);
  }
}


